<?php
namespace app\admin\controller;

use app\common\model\NavSolution as NavSolutionModel;
use app\common\controller\AdminBase;
use think\Db;

/**
 * 导航管理
 * Class Nav
 * @package app\admin\controller
 */
class Solutions extends AdminBase
{
	protected $nav_model;

    protected function _initialize()
    {
        parent::_initialize();
        $this->nav_solution  = new NavSolutionModel();
    }

    /**
     * 解决方案中心文章管理列表
     * @return mixed
     */
    public function index($keyword = '',$cid = '',$page = 1)
    {
    	$navlist = $this->nav_solution->findnewsid(); 
    	$newslist = $this->nav_solution->getnewslist($keyword,$cid,$page);
        return $this->fetch('index',['keyword'=>$keyword,'navlist'=>$navlist,'cid'=>$cid,'newslist'=>$newslist]);
    }

    /**
     * 添加解决方案
     * @return mixed
     */
    public function add()
    {
    	$navlist = $this->nav_solution->findnewsid();
    	$news_id = $this->nav_solution->findnewsid();
        return $this->fetch('add',['news_id'=>$news_id[0],'navlist'=>$navlist]);
    }

    /**
     * 保存解决方案
     */
    public function save()
    {
        if ($this->request->isPost()) {
            $data            = $this->request->param();
            $validate_result = $this->validate($data, 'NavNews');
           
            if (count($data) == 11){
            	$data['photo'] = setphoto($data['photo']);
            }
        
            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                if ($this->nav_solution->allowField(true)->save($data)) {
                    $this->success('保存成功');
                } else {
                    $this->error('保存失败');
                }
            }
        }
    }


     /**
     * 删除解决方案
     * @param int   $id
     * @param array $ids
     */
    public function delete($id = 0, $ids = [])
    {
        if($ids){        	
            $i = 0;
            for(;$i<count($ids);$i++){
                $list = $this->nav_solution->deletephoto($ids[$i]);
            }
            $id = array_merge($ids);
            $this->nav_solution->destroy($id);
            $this->success('删除成功');

        }

        if ($id) {
        	$list = $this->nav_solution->deletephoto($id);
            if ($this->nav_solution->destroy($id)) {
                $this->success('删除成功');
            } else {
                $this->error('删除失败');
            }
        } else {
            $this->error('请选择需要删除的文章');
        }
    }

     /**
     * 解决方案状态切换
     * @param array  $ids
     * @param string $type 操作类型
     */
    public function toggle($ids = [], $type = '')
    {
        $this->toggle1($ids,$type,$model = "nav_solution");
    }

    /**
     * 文章审核状态切换
     * @param array  $ids
     * @param string $type 操作类型
     */
    public function zhiding($ids = [], $type = '')
    { 	
    	$this->zhiding1($ids,$type,$model = "nav_solution");
    }

    /**
     * 编辑解决方案
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {
        $news = $this->nav_solution->find($id);
        $navlist = $this->nav_solution->findnewsid();
        if($news['photo']){
        	$news['photo'] = getphoto($news['photo']);
        }
        return $this->fetch('edit', ['news' => $news,'navlist'=>$navlist]);
    }
    /**
     * 更新文章
     * @param $id
     */
    public function update($id)
    {
        if ($this->request->isPost()) {
            $data            = $this->request->param();
            $validate_result = $this->validate($data, 'NavNews');

            if (count($data) == 11){
            	$data['photo'] = setphoto($data['photo']);
            }

            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                if ($this->nav_solution->allowField(true)->save($data, $id) !== false) {
                    $this->success('更新成功');
                } else {
                    $this->error('更新失败');
                }
            }
        }
    }
}